package com.vlong.town.controller.admin.information;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.information.Information;
import com.vlong.town.entity.labour.Labour;
import com.vlong.town.service.information.InformationService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * @author 购销信息
 */
@Controller
@RequestMapping("/information")
public class InformationController extends BaseController  {

	// Fields

	@Autowired
	private InformationService informationService;// 业务接口

	// Methods


   /**
	* 进入
	* @param pageNo 当前页码
	* @param pageSize 每页记录数
	* @return information/add_information
	*/
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String gotoInformationListPage(
			@RequestParam(required=false, value="pageNo") Integer pageNo,
			@RequestParam(required=false, value="pageSize") Integer pageSize,
			@RequestParam(required=false, value="title") String title
			) {
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<Information> queryList = informationService.queryList(null, 0, 2, title, null, null, null, null, null);
        PageInfo<Information> pageInfo = new PageInfo<Information>(queryList);
        PageTag<Information> pt = new PageTag<Information>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("status", 2);
        for(Information information : queryList) {
        	information.setContent(DataUtil.delHTMLTag(information.getContent(), 0));
        }
        request.setAttribute("informationList", queryList);
		return "admin/information/list_information";
	}

	
	/**
	* 进入
	* @param pageNo 当前页码
	* @param pageSize 每页记录数
	* @return information/add_information
	*/
	@RequestMapping(value="/listNo", method=RequestMethod.GET)
	public String gotoInformationListNoPage(
			@RequestParam(required=false, value="pageNo") Integer pageNo,
			@RequestParam(required=false, value="pageSize") Integer pageSize,
			@RequestParam(required=false, value="title") String title
			) {
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<Information>  queryList = new  ArrayList<Information>();
        List<Information> informationListOne = informationService.queryList(null, 0, 1, title, null, null, null, null, null);
        List<Information> informationListTwo = informationService.queryList(null, 0, 3, title, null, null, null, null, null);
        queryList.addAll(informationListOne);
        queryList.addAll(informationListTwo);
        PageInfo<Information> pageInfo = new PageInfo<Information>(queryList);
        PageTag<Information> pt = new PageTag<Information>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("status", 1);
        for(Information information : queryList) {
        	information.setContent(DataUtil.delHTMLTag(information.getContent(), 0));
        }
        request.setAttribute("informationList", queryList);
		return "admin/information/list_information";
	}
	
	
	
   /**
	* 进入
	* @return information/add_information
	*/
	@RequestMapping(value="/gotoAddPage", method=RequestMethod.GET)
	public String gotoInformationAddPage() {
		return "admin/information/add_information";
	}

   /**
	* 添加
	* 这里的参数需要根据实际情况自行修改
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/executeAddInformation", method=RequestMethod.POST)
	public DataMessage addInformation(
			@RequestParam(required=false, value="title") String title,
			@RequestParam(required=false, value="content") String content,
			@RequestParam(required=false, value="desc") String desc,
			@RequestParam(required=false, value="picture") String thumbnail
			) {
		DataMessage dm = new DataMessage();
		if(!validateStringProperty(title, 2, 20)){
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("标题参数错误");
		}else{
			this.informationService.add(null, 0, 2, title, content, desc, thumbnail, System.currentTimeMillis(), null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("数据添加成功");
		}
		return dm;
	}

   /**
	* 进入
	* @param informationId 主键值
	* @return information/edit_information
	*/
	@RequestMapping(value="/gotoEditPage/{informationId}")
	public String gotoInformationEditPage(@PathVariable(required=true, value="informationId") Long informationId) {
		Information information = this.informationService.queryById(informationId);
		request.setAttribute("information", information);
		request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + information.getThumbnail());
		return "admin/information/edit_information";
	}

   /**
	* 编辑
	* 这里的参数需要根据实际情况自行修改
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/executeEditInformation", method=RequestMethod.POST)
	public DataMessage editInformation(
			@RequestParam(required=false, value="informationId") String informationId,
			@RequestParam(required=false, value="title") String title,
			@RequestParam(required=false, value="content") String content,
			@RequestParam(required=false, value="desc") String desc,
			@RequestParam(required=false, value="picture") String thumbnail
			) {
		DataMessage dm = new DataMessage();
		if(!validateStringProperty(title, 2, 20)){
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("标题参数错误");
		}else{
			this.informationService.edit(Long.parseLong(informationId), null, null, title, content, desc, thumbnail, null, System.currentTimeMillis(),null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("数据修改成功");
		}
		return dm;
	}

   /**
	* 删除
	* @param informationId 主键值
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public DataMessage deleteInformation(@RequestParam(required=true, value="informationId") Long informationId) {
		DataMessage dm = new DataMessage();
		if(informationId != null && informationId > 0L) {
			this.informationService.edit(informationId, 1, null, null, null, null, null, null, null,null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("数据删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}
	
	
	/**
	* 批量删除
	* @param informationId 主键值
	* @return ->Json
	*/
	@ResponseBody
	@RequestMapping(value="/batchDelete", method=RequestMethod.POST)
	public DataMessage batchDelete(@RequestParam(required=true, value="informationIds") String informationIds) {
		DataMessage dm = new DataMessage();
		if(informationIds != null && informationIds !="") {
			String[] informationIdArray = informationIds.split(",");
			for (String informationId : informationIdArray) {
				this.informationService.edit(Long.parseLong(informationId), 1, null, null, null, null, null, null, null,null);
			}
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("数据删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}
	
	 	/**
		 * 通过
		 * @param informationId 主键值
		 * @return DataMessage->Json
		 */
		@ResponseBody
		@RequestMapping(value="/adoptInformation", method=RequestMethod.POST)
		public DataMessage adoptInformation(@RequestParam(required=true, value="informationId") Long informationId) {
			DataMessage dm = new DataMessage();
			if(informationId != null && informationId > 0L) {
				this.informationService.edit(informationId, null, 2, null, null, null, null, null, null,null);
				dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
				dm.setMessage("数据通过成功");
			} else {
				dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("参数错误");
			}
			return dm;
		}
	
		/**
		 * 驳回
		 * @param informationId 主键值
		 * @return DataMessage->Json
		 */
		@RequestMapping(value="/rejectInformation")
		public String rejectInformation(@RequestParam(required=true, value="informationId") Long informationId) {
			request.setAttribute("informationId", informationId);
			return  "admin/information/reject_information";
		}
		
		
		/**
	     * 更新驳回原因
	     * @param hotelId 主键值
	     * @return DataMessage->Json
	     */
	    @ResponseBody
	    @PostMapping("/updateReason")
	    public DataMessage updateReason(
	    		@RequestParam(required=true, value="informationId") Long informationId,
	    		@RequestParam(required=true, value="reason") String reason
	    		) {
	        DataMessage dm = new DataMessage();
	        if(informationId != null && informationId > 0L) {
	        	this.informationService.edit(informationId, null, 3, null, null, null, null, null, null,reason);
	            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
	            dm.setMessage("驳回成功");
	        } else {
	            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
	            dm.setMessage("参数错误");
	        }
	        return dm;
	    }
		
	    /**
		 * 查看驳回原因
		 * @return
		 */
		@RequestMapping(value="/viewInformation")
		public  String  viewInformation(@RequestParam(required=true, value="informationId") Long informationId){
			Information information = informationService.queryById(informationId);
			request.setAttribute("information", information);
			return "admin/information/view_information";
		}
	    
	    
	    
	    
	    
		
	
}